package jnpf.property.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.property.entity.ZcInspectionPlanEntity;
import jnpf.property.service.ZcInspectionPlanService;
import jnpf.property.service.ZcInspectionTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 定时巡检生成任务
 */
@Slf4j
@Component
@EnableScheduling
public class GenerateInspectionTask {

    @Autowired
    private ZcInspectionPlanService planService;

    @Autowired
    private ZcInspectionTaskService taskService;

    /**
     * 每日 0点10分0秒根据巡检计划生成巡检任务
     */
    @Scheduled(cron = "0 10 0 * * ? ")
    public void inspection() {
        // 获取生效的任务计划,状态为生效,且日期在生效日期内
        DateTime now = DateUtil.date();
        LambdaQueryWrapper<ZcInspectionPlanEntity> planQueryWrapper = new LambdaQueryWrapper<>();
        planQueryWrapper.eq(ZcInspectionPlanEntity::getStatus, 1)
                .lt(ZcInspectionPlanEntity::getEffectDate, now)
                .gt(ZcInspectionPlanEntity::getLapseData, now);

        List<ZcInspectionPlanEntity> planList = planService.list(planQueryWrapper);

        if (CollUtil.isNotEmpty(planList)) {
            planList.forEach(plan -> {
                // 根据生效周期和当前时间比对,判断是否生成任务
                boolean isGenerate = false;

                List<String> planConfigs = new ArrayList<>();
                if (StrUtil.isNotEmpty(plan.getGenerateConfig())) {
                    planConfigs.addAll(Arrays.asList(plan.getGenerateConfig().split(",")));
                }

                if (plan.getCycle() == 0) {
                    isGenerate = true;
                } else if (plan.getCycle() == 1) {
                    // 按周,判断当前是周几。1代表周末,2代表周一类推
                    String week = String.valueOf(DateUtil.dayOfWeek(now) - 1);
                    // 获取配置,周的配置是 1,2,3。代表周几
                    if (planConfigs.contains(week)) {
                        isGenerate = true;
                    }
                } else if (plan.getCycle() == 2) {
                    // 按月,判断当前是几号.
                    String day = String.valueOf(DateUtil.dayOfMonth(now));
                    if (planConfigs.contains(day)) {
                        isGenerate = true;
                    }
                } else if (plan.getCycle() == 3) {
                    // 周期类型为年,配置信息位具体的月份日期. 此时配置为 MM.dd,MM.dd
                    String monthDay = DateUtil.format(now, "MM.dd");
                    if (planConfigs.contains(monthDay)) {
                        isGenerate = true;
                    }
                }

                if (isGenerate) {
                    log.info("{}开始生成巡检任务", plan.getPlanName());
                    taskService.generateTask(plan.getId());
                }

            });
        }

    }

}
